package controller;

import javax.swing.ImageIcon;

import model.Layer;
import utils.ChooserUtils;
import view.MainFrame;
import dialog.FontChooser;
import dialog.NewColorLayerDialog;

/**
 * LayeredPane Controller
 * 
 * @author Hugo
 * 
 */
public class LayeredPaneController {

	/**
	 * remove the selectedLayer
	 */
	public static void removeSelectedLayer() {
		MainFrame
				.getInstance()
				.getDrawingPane()
				.removeLayer(
						MainFrame.getInstance().getDrawingPane()
								.getSelectedLayer());
	}

	/**
	 * create a new Layer from an image choosing via a JFileChooser <br>
	 * resize the image if its too big
	 */
	public static void createAnImageLayer() {
		String filePath = ChooserUtils.selectImageFile();
		if (!filePath.equals(ChooserUtils.cancelMsg)) {
			ImageIcon image = new ImageIcon(filePath);
			String fileName = filePath.substring(
					filePath.lastIndexOf("\\") + 1, filePath.lastIndexOf("."));
			image.setDescription(fileName);
			MainFrame.getInstance().getDrawingPane().addLayer(new Layer(image));
		}
	}

	/**
	 * create a new Layer with a Text <br>
	 * Choose text option via a FontChooser
	 */
	public static void createATextLayer() {
		new FontChooser();
	}

	/**
	 * create a new Layer fill with a color
	 */
	public static void createAColorLayer() {
		new NewColorLayerDialog();
	}

	/**
	 * fastest way to create a transparent full size layer
	 */
	public static void createAnEmptyLayer() {
		MainFrame.getInstance().getDrawingPane().addLayer(new Layer());
	}

	/**
	 * fusion 2 layers <br>
	 * draw the first on the second
	 * 
	 * @param first
	 * @param second
	 */
	public static void fusionLayers(int first, int second) {
		if (first != second) {

			Layer firstLayer = (Layer) MainFrame.getInstance().getDrawingPane()
					.getList().getElementAt(first);
			Layer secondLayer = (Layer) MainFrame.getInstance()
					.getDrawingPane().getList().getElementAt(second);
			firstLayer.setAlpha(255);
			secondLayer.setAlpha(255);
			MainFrame.getInstance().getDrawingPane()
					.fusionLayers(firstLayer, secondLayer);
			MainFrame.getInstance().getDrawingPane().removeLayer(firstLayer);
		}
	}
}
